package com.inei.evaluaciondelacapacitacion.model.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.evaluaciondelacapacitacion.model.connection.UtilDB;
import com.inei.evaluaciondelacapacitacion.model.entity.Respuesta2;

public class Respuesta2Dao implements ICRUD<Respuesta2> {

	UtilDB util;
	ContentValues cv;
	Cursor c;
	SQLiteDatabase db;
	Context context;
	Exception e;

	public Respuesta2Dao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	@Override
	public Respuesta2 buscar(Respuesta2 o) throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getReadableDatabase();
		db.beginTransaction();

		c = db.rawQuery("select * from respuesta2 where respuesta_id=?",
				new String[] { o.getRespuesta_id() + "" });

		if (c.moveToFirst()) {
			o.setFormato_id(c.getInt(c.getColumnIndex("formato_id")));

			o.setApe_paterno(c.getString(c.getColumnIndex("ape_paterno")));
			o.setApe_materno(c.getString(c.getColumnIndex("ape_materno")));
			o.setNombre(c.getString(c.getColumnIndex("nombre")));
			o.setDni(c.getString(c.getColumnIndex("dni")));

			o.setSede(c.getString(c.getColumnIndex("sede")));
			o.setP11(c.getString(c.getColumnIndex("P11")));
			o.setP12(c.getString(c.getColumnIndex("P12")));
			o.setP13(c.getString(c.getColumnIndex("P13")));
			o.setP14(c.getString(c.getColumnIndex("P14")));
			o.setP15(c.getString(c.getColumnIndex("P15")));
			o.setP16(c.getString(c.getColumnIndex("P16")));
			o.setP1_obs(c.getString(c.getColumnIndex("P1_obs")));
			o.setP21(c.getString(c.getColumnIndex("P21")));
			o.setP22(c.getString(c.getColumnIndex("P22")));
			o.setP221(c.getString(c.getColumnIndex("P221")));
			o.setP221_otro(c.getString(c.getColumnIndex("P221_otro")));
			o.setP23(c.getString(c.getColumnIndex("P23")));
			o.setP24(c.getString(c.getColumnIndex("P24")));
			o.setP25(c.getString(c.getColumnIndex("P25")));
			o.setP251a(c.getString(c.getColumnIndex("P251a")));
			o.setP251b(c.getString(c.getColumnIndex("P251b")));
			o.setP251c(c.getString(c.getColumnIndex("P251c")));
			o.setP2_obs(c.getString(c.getColumnIndex("P2_obs")));
			o.setP31(c.getString(c.getColumnIndex("P31")));
			o.setP311a(c.getString(c.getColumnIndex("P311a")));
			o.setP311b(c.getString(c.getColumnIndex("P311b")));
			o.setP311c(c.getString(c.getColumnIndex("P311c")));
			o.setP311d(c.getString(c.getColumnIndex("P311d")));
			o.setP311_otro(c.getString(c.getColumnIndex("P311_otro")));
			o.setP4(c.getInt(c.getColumnIndex("P4")));
			o.setObservacion(c.getString(c.getColumnIndex("observacion")));
			o.setSugerencia(c.getString(c.getColumnIndex("sugerencia")));
			o.setFec_crea(c.getString(c.getColumnIndex("fec_crea")));
			o.setFec_modi(c.getString(c.getColumnIndex("fec_modi")));

			db.setTransactionSuccessful();
		} else {
			e = new Exception("No hay resultados para su busqueda");
		}

		cerrar();
		if (e != null) {
			throw e;
		}
		return o;
	}

	@Override
	public List<Respuesta2> leer() throws Exception {
		List<Respuesta2> lista = null;
		e = null;
		db = util.getReadableDatabase();
		db.beginTransaction();
		c = db.rawQuery("select * from respuesta2", null);

		if (c.moveToFirst()) {
			lista = new ArrayList<Respuesta2>();
			do {
				Respuesta2 o = new Respuesta2();
				o.setRespuesta_id(c.getInt(c.getColumnIndex("respuesta_id")));
				o.setFormato_id(c.getInt(c.getColumnIndex("formato_id")));

				o.setApe_paterno(c.getString(c.getColumnIndex("ape_paterno")));
				o.setApe_paterno(c.getString(c.getColumnIndex("ape_materno")));
				o.setNombre(c.getString(c.getColumnIndex("nombre")));
				o.setDni(c.getString(c.getColumnIndex("dni")));

				o.setSede(c.getString(c.getColumnIndex("sede")));
				o.setP11(c.getString(c.getColumnIndex("P11")));
				o.setP12(c.getString(c.getColumnIndex("P12")));
				o.setP13(c.getString(c.getColumnIndex("P13")));
				o.setP14(c.getString(c.getColumnIndex("P14")));
				o.setP15(c.getString(c.getColumnIndex("P15")));
				o.setP16(c.getString(c.getColumnIndex("P16")));
				o.setP1_obs(c.getString(c.getColumnIndex("P1_obs")));
				o.setP21(c.getString(c.getColumnIndex("P21")));
				o.setP22(c.getString(c.getColumnIndex("P22")));
				o.setP221(c.getString(c.getColumnIndex("P221")));
				o.setP221_otro(c.getString(c.getColumnIndex("P221_otro")));
				o.setP23(c.getString(c.getColumnIndex("P23")));
				o.setP24(c.getString(c.getColumnIndex("P24")));
				o.setP25(c.getString(c.getColumnIndex("P25")));
				o.setP251a(c.getString(c.getColumnIndex("P251a")));
				o.setP251b(c.getString(c.getColumnIndex("P251b")));
				o.setP251c(c.getString(c.getColumnIndex("P251c")));
				o.setP2_obs(c.getString(c.getColumnIndex("P2_obs")));
				o.setP31(c.getString(c.getColumnIndex("P31")));
				o.setP311a(c.getString(c.getColumnIndex("P311a")));
				o.setP311b(c.getString(c.getColumnIndex("P311b")));
				o.setP311c(c.getString(c.getColumnIndex("P311c")));
				o.setP311d(c.getString(c.getColumnIndex("P311d")));
				o.setP311_otro(c.getString(c.getColumnIndex("P311_otro")));
				o.setP4(c.getInt(c.getColumnIndex("P4")));
				o.setObservacion(c.getString(c.getColumnIndex("observacion")));
				o.setSugerencia(c.getString(c.getColumnIndex("sugerencia")));
				o.setFec_crea(c.getString(c.getColumnIndex("fec_crea")));
				o.setFec_modi(c.getString(c.getColumnIndex("fec_modi")));
				lista.add(o);
			} while (c.moveToNext());
			db.setTransactionSuccessful();
		} else {
			e = new Exception("No hay registros");
		}

		cerrar();
		if (e != null) {
			throw e;
		}
		// TODO Auto-generated method stub
		return lista;
	}

	@Override
	public int crear(Respuesta2 o) throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv = new ContentValues();
		cv.put("formato_id", 4);
		cv.put("ape_paterno", o.getApe_paterno());
		cv.put("ape_materno", o.getApe_materno());
		cv.put("nombre", o.getNombre());
		cv.put("dni", o.getDni());
		cv.put("sede", o.getSede());
		cv.put("P11", o.getP11());
		cv.put("P12", o.getP12());
		cv.put("P13", o.getP13());
		cv.put("P14", o.getP14());
		cv.put("P15", o.getP15());
		cv.put("P16", o.getP16());
		cv.put("P1_obs", o.getP1_obs());
		cv.put("P21", o.getP21());
		cv.put("P22", o.getP22());
		cv.put("P221", o.getP221());
		cv.put("P221_otro", o.getP221_otro());
		cv.put("P23", o.getP23());
		cv.put("P24", o.getP24());
		cv.put("P25", o.getP25());
		cv.put("P251a", o.getP251a());
		cv.put("P251b", o.getP251b());
		cv.put("P251c", o.getP251c());
		cv.put("P2_obs", o.getP2_obs());
		cv.put("P31", o.getP31());
		cv.put("P311a", o.getP311a());
		cv.put("P311b", o.getP311b());
		cv.put("P311c", o.getP311c());
		cv.put("P311d", o.getP311d());
		cv.put("P311_otro", o.getP311_otro());
		cv.put("P4", o.getP4());
		cv.put("observacion", o.getObservacion());
		cv.put("sugerencia", o.getSugerencia());
		cv.put("fec_crea", obtenerFecha());
		cv.put("fec_modi", obtenerFecha());

		int id = (int) db.insert("respuesta2", null, cv);

		if (id > 0) {
			db.setTransactionSuccessful();
		} else {
			e = new Exception("UPDATE 1: error en el dao");
		}

		cerrar();
		if (e != null)
			throw e;

		return id;
	}

	@Override
	public int eliminar(Respuesta2 o) throws Exception {
		// TODO Auto-generated method stub
		
		return 0;
	}

	@Override
	public int actualizar(Respuesta2 o) throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv = new ContentValues();
		cv.put("ape_paterno", o.getApe_paterno());
		cv.put("ape_materno", o.getApe_materno());
		cv.put("nombre", o.getNombre());
		cv.put("dni", o.getDni());
		cv.put("sede", o.getSede());
		cv.put("P11", o.getP11());
		cv.put("P12", o.getP12());
		cv.put("P13", o.getP13());
		cv.put("P14", o.getP14());
		cv.put("P15", o.getP15());
		cv.put("P16", o.getP16());
		cv.put("P1_obs", o.getP1_obs());
		cv.put("P21", o.getP21());
		cv.put("P22", o.getP22());
		cv.put("P221", o.getP221());
		cv.put("P221_otro", o.getP221_otro());
		cv.put("P23", o.getP23());
		cv.put("P24", o.getP24());
		cv.put("P25", o.getP25());
		cv.put("P251a", o.getP251a());
		cv.put("P251b", o.getP251b());
		cv.put("P251c", o.getP251c());
		cv.put("P2_obs", o.getP2_obs());
		cv.put("P31", o.getP31());
		cv.put("P311a", o.getP311a());
		cv.put("P311b", o.getP311b());
		cv.put("P311c", o.getP311c());
		cv.put("P311d", o.getP311d());
		cv.put("P311_otro", o.getP311_otro());
		cv.put("P4", o.getP4());
		cv.put("observacion", o.getObservacion());
		cv.put("sugerencia", o.getSugerencia());
		cv.put("fec_modi", obtenerFecha());

		int id =  db.update("respuesta2", cv, "respuesta_id=?", new String[]{""+o.getRespuesta_id()});

		if (id > 0) {
			db.setTransactionSuccessful();
		} else {
			e = new Exception("INSERT 1: error en el DAO");
		}

		cerrar();
		if (e != null)
			throw e;

		return id;
	}
	public String obtenerFecha(){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(c.getTime());
	}
	
	@Override
	public void cerrar() {
		if (c != null) {
			c.close();
		}
		if (cv != null) {
			cv.clear();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}

}
